// const mongoose = require("mongoose");
var express = require('express');
// 创建了一个路由器
var router = express.Router();

const { get, addOne, changeState, updataOneSeckill, selectSEckills, selectFromToType, selectFromchildType, delSckill, findOne, selectSEckillsAndPage } = require("../api/seckill");

// 获取秒杀商品
router.get('/', async function (req, res, next) {
  // 解构当前页和每页显示条数
  const { curPage, eachPage } = req.query;
  const data = await get({ curPage, eachPage });

  res.send(data);
});
// 新增秒杀商品
router.post('/addseckill', async function (req, res, next) {
  // 解构当前页和每页显示条数
  const data = req.body;
  const result = await addOne(data);

  res.send(result);
});

// 修改商品在售状态商品
router.post('/changeState', async function (req, res, next) {
  // 解构当前页和每页显示条数
  const { _id, State } = req.body;
  const result = await changeState({ _id, State });
  res.send(result);
});

// 修改信息
router.patch('/updataOne', async (req, res, next) => {
  const data = req.body;
  const result = await updataOneSeckill(data);
  res.send(result)
})



// 秒杀活动的查询   接受参数 是一个对象 { name, Start_time } 最多为这两个值   可以传其中一个， 日期格式:YYYY-MM-DD HH:mm:ss
router.get("/selectSEckills", async (req, res, next) => {
  const data = req.query;
  const result = await selectSEckills(data)
  res.send(result)
})

// 秒杀活动的查询   接受参数 是一个对象 { { name, Start_time, curPage,eachPage } } name, Start_time, 可以传其中一个， 日期格式:YYYY-MM-DD HH:mm:ss
router.get("/selectSEckillsAndPage", async (req, res, next) => {
  const data = req.query;
  const result = await selectSEckillsAndPage(data)
  res.send(result)
})


// 根据父类型来查询数据

router.get("/selectFromToType", async (req, res, next) => {
  const { Father_id } = req.query;
  const result = await selectFromToType({ Father_id })
  res.send(result)
})

// 根据子类型查询数据
router.get("/selectFromchildType", async (req, res, next) => {
  const { child_id } = req.query;
  const result = await selectFromchildType({ child_id })
  res.send(result)
})


// 删除活动
router.delete("/delSeckill", async (req, res, next) => {
  const { _id } = req.query;
  const result = await delSckill({ _id })
  res.send(result)
})
// 根据活动id查询整条数据
router.get("/findOne", async (req, res, next) => {
  const { _id } = req.query;
  const result = await findOne({ _id })
  res.send(result)
})

// 暴露接口
module.exports = router;
